﻿using NetMVC.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NetMVC.Web.Controllers
{
    public class DramaController : Controller
    {
        ApplicationDbContext db = new ApplicationDbContext();
        // GET: Drama
        public ActionResult Index()
        {
            var dramas = db.Dramas;
            return View(dramas);
        }

        // GET: Drama/Details/5
        public ActionResult Details(int id)
        {
            var drama = db.Dramas.Find(id);
            return View(drama);
        }

        // GET: Drama/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Drama/Create
        [HttpPost]
        public ActionResult Create(Domain.Dramas.Drama drma)
        {
            try
            {
                // TODO: Add insert logic here

                db.Dramas.Add(drma);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Drama/Edit/5
        public ActionResult Edit(int id)
        {
            var drama = db.Dramas.Find(id);
            return View(drama);
        }

        // POST: Drama/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, Domain.Dramas.Drama drama)
        {
            try
            {
                // TODO: Add update logic here
                var _drama = db.Dramas.First(o => o.id == drama.id);
                db.Entry<Domain.Dramas.Drama>(_drama).CurrentValues.SetValues(drama);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Drama/Delete/5
        public ActionResult Delete(int id)
        {
            var drama = db.Dramas.Find(id);
            return View(drama);
        }

        // POST: Drama/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                var drama = db.Dramas.Find(id);
                db.Dramas.Remove(drama);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Drama/Create
        public ActionResult CategoryList()
        {
            var list = db.DramaCategorys;
            return View(list);
        }

        // GET: Drama/Create
        public ActionResult CategoryCreate()
        {
            return View();
        }

        // POST: Drama/Create
        [HttpPost]
        public ActionResult CategoryCreate(Domain.Dramas.DramaCategory drmaCategory)
        {
            try
            {
                // TODO: Add insert logic here

                db.DramaCategorys.Add(drmaCategory);
                db.SaveChanges();
                return RedirectToAction("CategoryList");
            }
            catch
            {
                return View();
            }
        }

        // GET: Drama/Create
        public ActionResult CategoryEdit(int id)
        {
            var drmaCategory = db.DramaCategorys.Find(id);
            return View(drmaCategory);
        }

        // POST: Drama/Create
        [HttpPost]
        public ActionResult CategoryEdit(Domain.Dramas.DramaCategory drmaCategory)
        {
            try
            {
                // TODO: Add insert logic here

                var _drmaCategory = db.DramaCategorys.First(o => o.id == drmaCategory.id);
                db.Entry<Domain.Dramas.DramaCategory>(_drmaCategory).CurrentValues.SetValues(drmaCategory);
                db.SaveChanges();
                return RedirectToAction("CategoryList");
            }
            catch
            {
                return View();
            }
        }
    }
}
